package net.whg.java;

/**
 * 对象克隆：默认浅拷贝
 * 字段的值被复制，但如果字段是引用类型，那么新对象的字段仍然指向原对象的同一内存地址。
 */
public class ObjectCloneTest implements Cloneable{

    private String name;

    private Student student;

    public ObjectCloneTest(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        ObjectCloneTest obj = new ObjectCloneTest("whg");
        Student student = new Student(18);
        obj.setStudent(student);
        Object cloneObj = obj.clone();
        ObjectCloneTest cloneObj2 = (ObjectCloneTest) cloneObj;
        obj.setName("xxx");
        obj.getStudent().setAge(20);
        System.out.println(cloneObj2.name);
        System.out.println(cloneObj2.getStudent().getAge());
    }

    static class Student {

        private int age;

        public Student(int age) {
            this.age = age;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}
